package com.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class OtherUtils {
	
	public final static String filelocation = "conf/configurableValues.properties";
	Properties properties = null;
	public Properties loadPropertiesFile(){
		properties = new Properties();
		try {
			String confLocation =  System.getProperty("confLocation", "");
			properties.load(getInputStreamResource(filelocation, confLocation));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}
	
	/**
	 * Get a file/input stream resource. Firstly, it attempts to find the resource as a file in the
	 * configLocation. If it does not exist there, then it
	 * attempts to load it from the JAR file within which this class exists.
	 *
	 * @param file - the relative path of the file to be loaded
	 * @param configLocation - the external config location
	 * @return input stream for the specified file
	 */
	protected InputStream getInputStreamResource(String file, String configLocation) {
		InputStream is = null;
		try {
			is = new FileInputStream(configLocation + file);
		} catch (FileNotFoundException e) {
			is = getClass().getClassLoader().getResourceAsStream(file);
			if (is == null) {
				try {
					is = new FileInputStream(file);
				} catch (FileNotFoundException ex) {
				}
			}
		}
		return is;
	}

}
